// pagesProperty/pages/asset/check/list/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"),
formatTime = require("../../../../../component/common.js");
import Toast from '../../../../../component/vant/toast/toast';
import Dialog from '../../../../../component/vant/dialog/dialog';
var common_Js = require("../../../../../component/common.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    hasData: false,
    pageIndex: 1,
    pageSize: 10,
    assetList:[],
    borrowRecord:[],
    checkState:0,
    searchValue:'',
    currentTab:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.onHttpInit();
  },
  onShow(){
    console.log('this.data.currentTab', this.data.currentTab)
    this.data.pageIndex = 1;
    // this.data.currentTab = detail.index
    this.setData({
      assetList : [],
      borrowRecord:[],
    })
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },
  handleChange({detail}){    
    console.log('detail',detail)
    this.data.pageIndex = 1;
    this.setData({
      assetList : [],
      borrowRecord:[],
      currentTab: detail.index
    })
    this.data.currentTab = detail.index
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },  
  onSearch(val){
    console.log('onSearch',val);
    this.data.searchValue = val.detail;
    this.data.pageIndex = 1;
    this.setData({
      assetList: [],
      borrowRecord:[],
    })
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.data.pageIndex = 1;
    this.setData({
      assetList: [],
      borrowRecord:[],
    })
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/list', {
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      checkState: that.data.checkState,
      asset_type:1,
      searchValue: that.data.searchValue,
    }, function (res) {
      console.log("获取资产信息", res)
      if (res.data.code == 0) {
        var assetList = res.data.data.assetList;
        if(assetList.length > 0){
          that.data.pageIndex += 1;
          for (var index in assetList) {
            that.data.assetList.splice(that.data.assetList.length, 0, assetList[index]);
          }
          that.setData({
            hasData: true,
            assetList: that.data.assetList
          })
        }
      }else{
        // Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  onHttpInitMyBorrow: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/borrow/list', {
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      // checkState: that.data.checkState,
      // searchValue: that.data.searchValue,
    }, function (res) {
      console.log("获取我的借用资产信息", res)
      if (res.data.code == 0) {
        var recordList = res.data.data.recordList;
        if(recordList.length > 0){
          that.data.pageIndex += 1;
          for (var index in recordList) {
            that.data.borrowRecord.splice(that.data.borrowRecord.length, 0, recordList[index]);
          }
          that.setData({
            hasData: true,
            borrowRecord: that.data.borrowRecord
          })
        }
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  goDetail(e){
    var assetId = e.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '../index?id=' + assetId+'&from=click',
    })
  },
  scanCode(e){
    console.log('scanCode e',e)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {}
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            json_param = common_Js.getQueryVariable(q);
            if(qrcontent.indexOf('asset/borrow') >= 0){//借用
              wx.navigateTo({ url: '/pagesProperty/pages/asset/borrow/index?id=' + json_param.id})
            }else{
              Toast('暂未支持其他扫码功能')
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  goCall(e){
    console.log('goCall', e)
    let item = e.currentTarget.dataset.item;
    if(item.borrowRecord){
      let phone = item.borrowRecord.phoneNum;
      wx.makePhoneCall({
        phoneNumber: phone,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }
  },
  goReback(e){
    console.log('goReback', e)
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../reback/index?id=' + item.id,
    })
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      isLoading:true,
      assetList:[],
      borrowRecord:[],
    })
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    c.push(e.rebackImg)
    common_Js.preImg(a, c)
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    if(this.data.currentTab == 0){
      this.onHttpInit();
    }else{
      this.onHttpInitMyBorrow();
    }
  },
})